/**
 * 
 */
package com.Adapters.Items;


import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.ColorDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import com.Animations.AnimUtil;
import com.Animations.AnimUtil.AnimationCompleted;
import com.Animations.AnimationLists;
import com.Data.Post;
import com.Init.ImageDownloaderInit;
import com.Managers.PictureManagerClass;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.shareup.R;


/**
 * @author Chris
 * 
 */
public class ItemUpload implements ItemPost {

    public static boolean wasDestroyed;

    private Context context;

    private ViewHolder holder;

    private int imageWidth;

    private LayoutInflater inflater;

    private Post itemup;

    private ActionsUpload main;

    private ImageLoader imgLoader;

    class ViewHolder {

        public RelativeLayout container;

        public LinearLayout containerClose;

        public LinearLayout containerCover;

        public ImageView coverB;

        public ProgressBar progress;

        public ImageView removeB;

        public ImageView thumb;

    }

    public ItemUpload(int columnWidth, LayoutInflater inflater,
                Post itemup, ActionsUpload main, ImageLoader loader) {

        super();
        this.imageWidth = columnWidth;
        this.itemup = itemup;
        this.inflater = inflater;
        this.context = inflater.getContext();
        this.main = main;
        this.imgLoader = loader;

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#AnimateIn()
     */
    @Override
    public void AnimateIn() {

        AnimUtil.animateFadeIn(holder.thumb);

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#AnimateOut(com.Animations.AnimUtil.
     * AnimationCompleted)
     */
    @Override
    public void AnimateOut(AnimationCompleted callback) {

        AnimUtil.PostOut(context, holder.container, callback);

    }

    public void changeCover() {

    }

    public void changePin() {

    }

    public void changePrivacy() {

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#clearAnimatonList()
     */
    @Override
    public void clearAnimatonList() {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#delete()
     */
    @Override
    public void delete() {

        main.remove(this);
        holder.container.setVisibility(View.VISIBLE);

    }

    public void display(ImageView image, String url, final ProgressBar progressbar) {

        imgLoader.displayImage(ImageDownloader.Scheme.FILE.wrap(url), image, ImageDownloaderInit.getOptionsForGrid(), new ImageLoadingListener() {

            @Override
            public void onLoadingCancelled(String imageUri, View view) {

            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

                progressbar.setVisibility(View.GONE);

                if (loadedImage != null) {
                    ImageView image = (ImageView) view;
                    boolean firstDisplay = !AnimationLists.displayedThumbs.contains(imageUri);
                    if (firstDisplay) {
                        AnimationLists.displayedImages.add(imageUri);
                        AnimateIn();

                        image.setVisibility(View.VISIBLE);

                    }

                }
            }

            @Override
            public void onLoadingFailed(String imageUri, View view, FailReason failReason) {

                progressbar.setVisibility(View.GONE);

            }

            @Override
            public void onLoadingStarted(String imageUri, View view) {

                // progressbar.setVisibility(View.VISIBLE); // set the
                // spinner
                // visible
            }

        }, new ImageLoadingProgressListener() {

            @Override
            public void onProgressUpdate(String imageUri, View view, int current, int total) {

                progressbar.setVisibility(View.VISIBLE);
                progressbar.setMax(total);
                progressbar.setProgress(current);

            }
        });

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getCommentsCount()
     */
    @Override
    public int getCommentsCount() {

        // TODO Auto-generated method stub
        return 0;
    }

    public Post getItemup() {

        return this.itemup;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getLikesCount()
     */
    @Override
    public int getLikesCount() {

        // TODO Auto-generated method stub
        return 0;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPostId()
     */
    @Override
    public int getPostId() {

        return itemup.getID();
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPublicationDate()
     */
    @Override
    public String getPublicationDate() {

        // TODO Auto-generated method stub
        return null;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getView(android.view.View)
     */
    @Override
    public View getView(View v, int position) {

        if (v == null) {

            holder = new ViewHolder();
            v = inflater.inflate(R.layout.item_upload, null);

            holder.container = (RelativeLayout) v.findViewById(R.id.container);
            holder.thumb = (ImageView) v.findViewById(R.id.thumb);
            holder.removeB = (ImageView) v.findViewById(R.id.remove_button);
            holder.containerClose = (LinearLayout) v.findViewById(R.id.container_close);
            holder.containerCover = (LinearLayout) v.findViewById(R.id.container_cover);
            holder.coverB = (ImageView) v.findViewById(R.id.cover_button);
            holder.progress = (ProgressBar) v.findViewById(R.id.loading_progress);

            if (imageWidth != 0) {
                holder.thumb.setScaleType(ImageView.ScaleType.CENTER_CROP);
                holder.thumb.setLayoutParams(new RelativeLayout.LayoutParams(imageWidth, imageWidth));

            }
            else {
                holder.containerClose.setVisibility(View.GONE);
                Bitmap bmp = BitmapFactory.decodeFile(itemup.getPathFull());
                int w = bmp.getWidth();
                int h = bmp.getHeight();
                bmp = null;

                PictureManagerClass.scalePostImage(w, h, holder.thumb);
            }

            v.setTag(holder);

        }
        else {
            holder = (ViewHolder) v.getTag();

        }

        if (!itemup.isCover())
            holder.containerCover.setVisibility(View.INVISIBLE);

        setTexts();

        return v;
    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getViewType()
     */
    @Override
    public int getViewType() {

        return RowType.ALBUM.ordinal();
    }

    public void removeAsCover() {

        if (itemup.isCover()) {
            holder.containerCover.setVisibility(View.GONE);
            itemup.setCover(false);

        }

    }

    public void setAsCover() {

        if (!itemup.isCover() && main.getAlbumId() != 0) {
            itemup.setCover(true);
            holder.containerCover.setVisibility(View.VISIBLE);
            main.setAsCover(this.getPostId());
        }
        else
            if (itemup.isCover())
                removeAsCover();

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#setCommentsCount(int)
     */
    @Override
    public void setCommentsCount(int count) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#setLikesCount(int)
     */
    @Override
    public void setLikesCount(int count) {

        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#updatePublicationDate(java.lang.String)
     */
    @Override
    public void updatePublicationDate(String newDate) {

        // TODO Auto-generated method stub

    }

    private void setTexts() {

        holder.container.setOnClickListener(new onCoverClick(this));
        // holder.container.setOnLongClickListener(onUploadItenLongClick);
        holder.removeB.setOnClickListener(new onCloseClick(this));
        holder.thumb.setTag(itemup.getPathFull());

        holder.thumb.setImageDrawable(new ColorDrawable(context.getResources().getColor(R.color.main)));

        if (itemup.isCover())
            setAsCover();

        if (itemup.getThumb() == null)
            display(holder.thumb, itemup.getPathFull(), holder.progress);
        else {
            holder.thumb.setImageBitmap(itemup.getThumb());
            holder.containerClose.setVisibility(View.INVISIBLE);
            holder.progress.setVisibility(View.INVISIBLE);
        }

    }

    class onCloseClick implements OnClickListener {

        private ItemUpload item;

        public onCloseClick(ItemUpload item) {

            this.item = item;
        }

        @Override
        public void onClick(View v) {

            item.delete();
        }

    }

    class onCoverClick implements OnClickListener {

        private ItemUpload item;

        public onCoverClick(ItemUpload item) {

            this.item = item;
        }

        @Override
        public void onClick(View v) {

            item.setAsCover();
        }

    }

    /*
     * (non-Javadoc)
     * @see com.Adapters.Items.ItemPost#getPosition()
     */
    @Override
    public int getPosition() {

        // TODO Auto-generated method stub
        return 0;
    }

}
